package com.fram.controller.api;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseController;
import com.fram.entity.OrderShoppingcart;
import com.fram.service.api.v1.ShopCartService;

/**
 * @作者： 陳曉鬆 @时间： 2020年4月30日 - 上午10:20:49 @description：-> 购物车模块
 */
@RestController
@RequestMapping("/api/{version}/cart")
public class ShopCartController extends BaseController {

	private static Logger LOG = LoggerFactory.getLogger(ShopCartController.class);

	@Resource(name = "shopCartService")
	private ShopCartService shopCartService;

	/** 加入购物车 */
	@ApiVersion(1.0)
	@RequestMapping("auth/add")
	public ResponseMsg addShopCart(@RequestBody OrderShoppingcart request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			shopCartService.addShopCart(request);
		} catch (ProAffairException e) {
			LOG.error("-购物车-加入购物车出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-购物车-加入购物车异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 删除购物车商品数据 */
	@ApiVersion(1.0)
	@RequestMapping("auth/delete")
	public ResponseMsg addShopCart(@RequestBody List<Long> cartIds) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			shopCartService.deleteShopCart(cartIds);
		} catch (ProAffairException e) {
			LOG.error("-购物车-删除购物车商品数据出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-购物车-删除购物车商品数据异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 购物车修改数量 */
	@ApiVersion(1.0)
	@RequestMapping("auth/update")
	public ResponseMsg updateShopCart(@RequestBody OrderShoppingcart request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			msg.setData(shopCartService.updateShopCartNumber(request));
		} catch (ProAffairException e) {
			LOG.error("-购物车-修改购物车商品数据出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-购物车-修改购物车商品数据异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 购物车商品数据 */
	@ApiVersion(1.0)
	@RequestMapping("auth/list")
	public ResponseMsg list(@RequestBody PageBean<Long> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(shopCartService.searchShopCartList(request, getApiUserId()));
		} catch (ProAffairException e) {
			LOG.error("-购物车-查询购物车商品数据出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-购物车-查询购物车商品数据异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 购物车结算 */
	@ApiVersion(1.0)
	@RequestMapping("auth/shopCartSettlement")
	public ResponseMsg shopCartSettlement(@RequestBody List<Long> cartIds) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(shopCartService.shopCartSettlement(cartIds, getApiUserId()));
		} catch (ProException e) {
			LOG.error("-购物车-购物车结算出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-购物车-购物车结算异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

}
